using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GoDive.Contract;
using GoDive.Service;
using GoDive.Common;

namespace GoDive.App
{

    public class DestController : Controller
    {

        GoDiveDBContext db = new GoDiveDBContext();


        public ActionResult Index()
        {
            DestViewModel destModel = new DestViewModel();

            var banners = db.tbanner.FirstOrDefault(x => x.Type == (int)BannerTypeEnum.Destination);

            destModel.Banner = banners;

            var regions = db.tregion.Where(x => db.tdestination.Any(des => des.RegionID == x.ID))
                .Select(x => new
                {
                    ID = x.ID,
                    Name = x.Name,
                    EnName = x.EnName,
                    OrderIndex = x.OrderIndex
                })
                .OrderByDescending(x => x.OrderIndex)
                .ToList();

            var regionIDs = regions.Select(x => x.ID).ToList();
            var destinations = db.tdestination.Where(x => regionIDs.Contains(x.RegionID) && x.ParentID == 0)
                .Select(x => new
                {
                    ID = x.ID,
                    RegionID = x.RegionID,
                    Name = x.Name,
                    EnName = x.EnName,
                    IsHot = x.IsHot,
                    DataChangeTime = x.DataChangeTime
                })
                .OrderBy(x => x.DataChangeTime)
                .ToList();

            destModel.Regions = regions.Select(x => new tregion
            {
                ID = x.ID,
                Name = x.Name,
                EnName = x.EnName,
                Destinations = destinations.Where(des => des.RegionID == x.ID).Select(des => new tdestination
                {
                    ID = des.ID,
                    RegionID = des.RegionID,
                    Name = des.Name,
                    EnName = des.EnName,
                    DataChangeTime = des.DataChangeTime
                }).ToList()
            }).ToList();

            destModel.RecommendDes = destinations.Where(x => x.IsHot).Select(des => new tdestination
            {
                ID = des.ID,
                RegionID = des.RegionID,
                Name = des.Name,
                EnName = des.EnName
            }).Take(5).ToList();

            return View(destModel);
        }


        public ActionResult Detail(int id)
        {

            DestViewModel destModel = new DestViewModel();

            tdestination destination = db.tdestination.FirstOrDefault(x => x.ID == id);
            destModel.Destination = destination;

            if (destination == null)
            {
                return null;
            }

            var childDests = db.tdestination.Where(x => x.ParentID == destination.ID).Select(x => new
            {
                ID = x.ID,
                Name = x.Name,
                EnName = x.EnName,
                OrderIndex = x.OrderIndex
            }).ToList();

            childDests.Add(new
            {
                ID = destination.ID,
                Name = destination.Name,
                EnName = destination.EnName,
                OrderIndex = destination.OrderIndex
            });

            List<long> destIDList = childDests.Select(x => x.ID).ToList();

            var divesites = db.tdivesite.Where(x => destIDList.Contains(x.DestinationID))
                .Select(x => new
                {
                    ID = x.ID,
                    DestinationID = x.DestinationID,
                    Name = x.Name,
                    EnName = x.EnName,
                    Pic = x.Pic,
                    Summary = x.Summary,
                    Score = x.Score,
                    DifficultLevel = x.DifficultLevel,
                    DiveCategory = x.DiveCategory,
                    OrderIndex = x.OrderIndex,
                    DataChangeTime = x.DataChangeTime
                }).OrderByDescending(site => site.OrderIndex).ThenBy(site => site.DataChangeTime).ToList();

            var detinationDiveSites = childDests.Where(x => divesites.Any(site => site.DestinationID == x.ID))
                .Select(x => new tdestination
            {
                ID = x.ID,
                Name = x.Name,
                EnName = x.EnName,
                OrderIndex = x.OrderIndex,
                DiveSites = divesites.Where(site => site.DestinationID == x.ID)
                .Select(site => new tdivesite
                {
                    ID = site.ID,
                    DestinationID = site.DestinationID,
                    Name = site.Name,
                    EnName = site.EnName,
                    Pic = site.Pic,
                    Summary = site.Summary,
                    Score = site.Score,
                    DifficultLevel = site.DifficultLevel,
                    DiveCategory = site.DiveCategory
                }).ToList()
            }).OrderByDescending(x => x.OrderIndex).ToList();

            destModel.PhotoCounts = db.tdestinationalbum.Count(x => x.DestinationID == id);
            destModel.DestinationDiveSites = detinationDiveSites;

            return View(destModel);
        }

        public ActionResult Comment(int id)
        {

            DestViewModel destModel = new DestViewModel();

            tdestination destination = db.tdestination.FirstOrDefault(x => x.ID == id);
            destModel.Destination = destination;

            if (destination == null)
            {
                return null;
            }

            DestinationController destApiController = new DestinationController();
            destModel.DestinationComments = destApiController.GetCommentPagedList(id, 1, 10);

            return View(destModel);
        }

        public ActionResult Article(int id)
        {

            DestViewModel destModel = new DestViewModel();

            tdestination destination = db.tdestination.FirstOrDefault(x => x.ID == id);
            destModel.Destination = destination;

            if (destination == null)
            {
                return null;
            }

            ArticleController articleApiController = new ArticleController();
            destModel.DestinationArticles = articleApiController.GetPagedList(1, 10, 0,
                x => x.DestinationID == id);

            return View(destModel);
        }

        public ActionResult Traffic(int id)
        {
            DestViewModel destModel = new DestViewModel();

            tdestination destination = db.tdestination.FirstOrDefault(x => x.ID == id);
            destModel.Destination = destination;

            if (destination == null)
            {
                return null;
            }

            var traffics = db.tdesttraffic.Where(x => x.DestinationID == id).Select(x => new
            {
                Depart = x.Depart,
                Type = x.Type,
                Desc = x.Desc,
                OrderIndex = x.OrderIndex
            }).OrderBy(x => x.OrderIndex).AsEnumerable()
            .Select(x => new tdesttraffic
            {
                Depart = x.Depart,
                Type = x.Type,
                Desc = x.Desc
            }).ToList();

            destModel.DestinationTraffics = traffics;

            return View(destModel);
        }

    }
}
